package mercury.gui;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import mercury.Main;

public class MainGUI {

	private static MainGUI singleton;
	private JTextArea results = new JTextArea();
	private JTextArea logs = new JTextArea();
	
	public static MainGUI get() {
		return singleton;
	}
	
	public MainGUI() {
		singleton = this;
        JFrame frame = new JFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
//      Create a scrollable text area
        
        results.setEditable(false);
     //   results.setLineWrap(true);
        results.setRows(20);
        
        logs.setEditable(false);
     //   logs.setLineWrap(true);
        JScrollPane resultsscroll = new JScrollPane(results);
        JScrollPane logsscroll = new JScrollPane(logs);
        
//      Create a top-bottom split pane
        JSplitPane vpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, resultsscroll, logsscroll);

        // Add button to the frame
        frame.getContentPane().add(vpane, BorderLayout.CENTER);

        // Set initial size
        int frameWidth = 900;
        int frameHeight = 800;
        frame.setSize(frameWidth, frameHeight);

        // Show the frame
        frame.setVisible(true);
        Main main = new Main();
        
        if(main.isAdminFeedKeySet()) {
        	
        } else {
        
	        if(!main.isUserSet()) {
	       	 String inputValue = JOptionPane.showInputDialog("Login");
	       	main.setUser(inputValue);
	       }
	        
	        if(!main.isPasswordSet()) {
	        	 String inputValue = JOptionPane.showInputDialog("Password");
	        	main.setPassword(inputValue);
	        }
        }
        
        if((main.isPasswordSet() && main.isUserSet()) || main.isAdminFeedKeySet()) {
        	main.run();
        } else {
        	String msg = "Error: Username/Password or Admin Feed key not set correctly";
        	results.append(msg);
        	logs.append(msg);
        }
	}
	
	public void appendResults(String str) {
		results.append(str);
	}
	
	public void appendLog(String str) {
		logs.append(str);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MainGUI();

	}

}
